﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using CodeTier.DAL;

namespace CodeTier.BUL
{
    public class SanPhamBUS
    {
        public int MaSP { get; set; }
        public int MaDM { get; set; }
        public string TenSP { get; set; }
        public string MoTa { get; set; }
        public int DonGia { get; set; }
        public byte[] HinhAnh { get; set; }
        public int SLTonKho { get; set; }
        public DateTime NgayDang { get; set; }
        public string TinhTrang { get; set; }
        

        public SanPhamBUS() { }

        public SanPhamBUS(int masp) 
        {
           
        }

        public static bool insert(SanPhamBUS spBus)
        {
            SanPham dm = new SanPham
            {
                MaDM = spBus.MaDM,
                TenSP = spBus.TenSP,
                MoTa = spBus.MoTa,
                DonGia = spBus.DonGia,
                HinhAnh = spBus.HinhAnh,
                SLTonKho = spBus.SLTonKho,
                NgayDang = spBus.NgayDang,
                TinhTrang = spBus.TinhTrang,
            };
            CommonDAO.XDMH.SanPhams.InsertOnSubmit(dm);
            try
            {
                CommonDAO.XDMH.SubmitChanges();
                CommonDAO.XDMH.Refresh(System.Data.Linq.RefreshMode.OverwriteCurrentValues, CommonDAO.XDMH.SanPhams);
                return true;
            }
            catch (Exception)
            {
                return false;
            }
        }

        public bool delete()
        {
            CommonDAO.XDMH.SanPhams.DeleteOnSubmit(getByIdNonConvert(this.MaSP));
            try
            {
                CommonDAO.XDMH.SubmitChanges();
                CommonDAO.XDMH.Refresh(System.Data.Linq.RefreshMode.OverwriteCurrentValues, CommonDAO.XDMH.SanPhams);
                return true;
            }
            catch (Exception)
            {
                return false;
            }            
        }

        public bool update()
        {
            var result = getByIdNonConvert(this.MaSP);
            result.MaSP = this.MaSP;
            result.MaDM = this.MaDM;
            result.MoTa = this.MoTa;
            result.TenSP = this.TenSP;
            result.SLTonKho = this.SLTonKho;
            if(this.HinhAnh != null)
                result.HinhAnh = this.HinhAnh;
            result.DonGia = this.DonGia;
            result.NgayDang = this.NgayDang;
            result.TinhTrang = this.TinhTrang;
            try
            {
                CommonDAO.XDMH.SubmitChanges();
                CommonDAO.XDMH.Refresh(System.Data.Linq.RefreshMode.OverwriteCurrentValues, CommonDAO.XDMH.SanPhams);
                return true;
            }
            catch (Exception)
            {
                return false;
            }
        }

        public static SanPham getByIdNonConvert(int masp)
        {
            var result = (from sp in CommonDAO.XDMH.SanPhams
                          where sp.MaSP == masp
                          select sp).SingleOrDefault();            
            return result;
        }

        public static SanPhamBUS getByIdConvert(int masp)
        {
            var result = getByIdNonConvert(masp);
            return convertUp(result);
        }

        public static SanPhamBUS convertUp(SanPham sp)
        {
            SanPhamBUS sp1 = new SanPhamBUS();
            sp1.MaSP = sp.MaSP;
            sp1.MaDM = sp.MaDM;
            sp1.MoTa = sp.MoTa;
            sp1.TenSP = sp.TenSP;
            sp1.HinhAnh = (byte[])(sp.HinhAnh).ToArray();
            sp1.SLTonKho = sp.SLTonKho;
            sp1.DonGia = Convert.ToInt32(sp.DonGia);
            sp1.NgayDang = sp.NgayDang;
            sp1.TinhTrang = sp.TinhTrang;
            return sp1;
        }        

        public static List<SanPhamBUS> getAll()
        {
            var results = (from sp in CommonDAO.XDMH.SanPhams
                          select sp);
            List<SanPhamBUS> lstDM = new List<SanPhamBUS>() ;
            foreach (var result in results)
            {
                SanPhamBUS spBus = new SanPhamBUS();
                spBus.MaSP = result.MaSP;
                spBus.MaDM = result.MaDM;
                spBus.MoTa = result.MoTa;
                spBus.TenSP = result.TenSP;
                spBus.HinhAnh = (byte[])(result.HinhAnh).ToArray();
                spBus.SLTonKho = result.SLTonKho;
                spBus.DonGia = Convert.ToInt32(result.DonGia);
                spBus.NgayDang = result.NgayDang;
                spBus.TinhTrang = result.TinhTrang;
                lstDM.Add(spBus);
            }
            return lstDM;
        }

        public static List<SanPhamBUS> getAllByDanhMuc(int MaDM)
        {
            var results = (from sp in CommonDAO.XDMH.SanPhams
                           where sp.MaDM == MaDM
                           select sp);
            List<SanPhamBUS> lstSP = new List<SanPhamBUS>();            
            foreach (var result in results)
            {
                SanPhamBUS spBus = new SanPhamBUS();
                spBus.MaSP = result.MaSP;
                spBus.MaDM = result.MaDM;
                spBus.MoTa = result.MoTa;
                spBus.TenSP = result.TenSP;
                spBus.HinhAnh = (byte[])(result.HinhAnh).ToArray();
                spBus.SLTonKho = result.SLTonKho;
                spBus.DonGia = Convert.ToInt32(result.DonGia);
                spBus.NgayDang = result.NgayDang;
                spBus.TinhTrang = result.TinhTrang;
                lstSP.Add(spBus);
            }
            return lstSP;
        }

    }
}
